/**
 * 查找DataRecords
 */
package com.flute.framework.data.preparer;

import org.apache.commons.lang.StringUtils;

import com.flute.framework.data.constants.Constants;
import com.flute.framework.data.mysql.MySQLConnectionUtil;
import com.flute.tools.data.DataIterator;
import com.flute.tools.data.DataRecord;
import com.flute.tools.data.mysql.NewMysqlHandler;

/**
 * @author Jazz
 * 
 */
public final class DataRecordsUtil {
	private DataRecordsUtil() {
		throw new RuntimeException("不能实例化" + DataRecordsUtil.class);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getBusinessCountByKey("1000179", "test");
	}

	public static String getBusinessCountByKey(String key,
			String tablePrefix) {
		NewMysqlHandler handler = MySQLConnectionUtil.getNewMysqlHandler();

		String sql = "SELECT business_count from "
				+ MySQLConnectionUtil.getAllRecordsTable(tablePrefix)
				+ " WHERE documentID='" + key + "'";
		DataIterator<DataRecord> it = handler.iterator(sql);

		if (it.hasNext()) {
			DataRecord dr = it.next();
			System.out.println(sql);
			it.close();
			it = null;
			return dr.getFieldValue(Constants.BUSINESS_COUNT);
		}
		it.close();
		it = null;

		return StringUtils.EMPTY;
	}

	public static DataRecord getDataRecordByKeyFromTable(String key,
			String table) {
		NewMysqlHandler handler = MySQLConnectionUtil.getNewMysqlHandler();

		DataIterator<DataRecord> it = handler.iterator("SELECT * from "
				+ MySQLConnectionUtil.getAllRecordsTable(table)
				+ " WHERE documentID='" + key + "'");

		if (it.hasNext()) {
			DataRecord dr = it.next();
			it.close();
			it = null;
			return dr;
		}
		it.close();
		it = null;

		return null;
	}
}
